﻿function ClientGame(user) {
    now.user = user;
    this.users = null;
    this.gameBoardUpdated = null;
    this.messageReceived = null;
    this.loginSuccess = null;
    this.loginFailure = null;
    this.userJoined = null;
    this.userLeft = null;
    this.playerDied = null;
    this.gameStarted = null;
    this.gameRestarted = null;
    this.hostGranted = null;
    this.gameLocked = null;
    this.gameUnlocked = null;
    this.keyboardController = null;
    this.queuedMove = User.NO_MOVE;
}

ClientGame.prototype = (function () {
    var that = this;

    return {
        constructor: ClientGame,

        internalGameBoardUpdated: function (updates, alienUpdate) {
            that.queuedMove = User.NO_MOVE;
            that.gameBoardUpdated(updates, alienUpdate);
        },

        getMyUser: function () {
            return this.users[now.user.id];
        },

        getUsers: function () {
            return this.users;
        },

        getUser: function (id) {
            return this.users[id];
        },

        getNumberOfUsers: function () {
            return Object.keys(this.users).length;
        },

        connect: function () {
            var that = this;

            now.loginSuccess = this.loginSuccess;
            now.loginFailure = this.loginFailure;
            now.userLeft = this.userLeft;
            now.userJoined = this.userJoined;
            now.messageReceived = this.messageReceived;
            now.gameBoardUpdated = this.internalGameBoardUpdated;
            now.playerDied = this.playerDied;
            now.gameOver = this.gameOver;
            now.gameStarted = this.gameStarted;
            now.gameRestarted = this.gameRestarted;
            now.hostGranted = this.hostGranted;
            now.gameLocked = this.gameLocked;
            now.gameUnlocked = this.gameUnlocked;
            now.updateUsers = function (users) { that.users = users; };
            now.login();

            this.keyBoardController = new KeyboardController({
                37: that.moveLeft,
                38: that.moveUp,
                39: that.moveRight,
                40: that.moveDown
            }, 10);
        },

        setMyId: function (id) {
            now.user.id = id;
        },

        sendMessage: function (message) {
            now.sendMessage(message);
        },

        moveUp: function () {
            if (this.queuedMove != User.MOVE_UP) {
                this.queuedMove = User.MOVE_UP;
                now.moveUp();
            }
        },

        moveRight: function () {
            if (this.queuedMove != User.MOVE_RIGHT) {
                this.queuedMove = User.MOVE_RIGHT;
                now.moveRight();
            }
        },

        moveDown: function () {
            if (this.queuedMove != User.MOVE_DOWN) {
                this.queuedMove = User.MOVE_DOWN;
                now.moveDown();
            }
        },

        moveLeft: function () {
            if (this.queuedMove != User.MOVE_LEFT) {
                this.queuedMove = User.MOVE_LEFT;
                now.moveLeft();
            }
        },

        startGame: function (alienInterval, hunterInterval, botInterval, aiAlienBots) {
            now.startGame(alienInterval, hunterInterval, botInterval, aiAlienBots);
        },

        restartGame: function () {
            now.gameRestarted = this.gameRestarted;
            now.restartGame();
        },

        generateBoard: function (fillFactor) {
            now.generateBoard(fillFactor);
        },

        generateLevel: function (levelNumber) {
            now.generateLevel(levelNumber);
        },

        lockGame: function () {
            now.lockGame();
        },

        unlockGame: function () {
            now.unlockGame();
        }
    };
})();